﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace DoAn
{
    public partial class frmDanhsachnhanvien : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
    
        public frmDanhsachnhanvien()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }
     

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbmnv.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbtnv.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbdc.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            tbsdt.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            tbemail.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            cbgt.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            conn.Open();
            cmd = new SqlCommand("SELECT * from nhanvien", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "nhanvien");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmnv.Text == "" || tbtnv.Text == "" || tbdc.Text == "" || tbsdt.Text == "" || tbemail.Text == "" || cbgt.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbmnv.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM nhanvien", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "nhanvien");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["manv"] = tbmnv.Text;
                    newRow["nvten"] = tbtnv.Text;
                    newRow["nvdiachi"] = tbdc.Text;
                    newRow["nvsdt"] = tbsdt.Text;
                    newRow["nvemail"] = tbemail.Text;
                    newRow["nvgioitinh"] = cbgt.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "nhanvien");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                tbmnv.Focus();

            } 
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "nhanvien");
            MessageBox.Show("Xóa thành công");
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
                String manv = tbmnv.Text;
            String nvten = tbtnv.Text;
            String nvdiachi = tbdc.Text;
            String nvsdt = tbsdt.Text;
            String nvemail = tbemail.Text;
            String nvgioitinh = cbgt.Text;
           

            //int mahd = Int32.Parse(mahopdong);
            int man = Int32.Parse(manv);
            int sdt = Int32.Parse(nvsdt);
           // int tongts = Int32.Parse(tongtaisan);
           // int sotien = Int32.Parse(sotienvay);
            conn.Open();
            //String sql = 
            cmd = new SqlCommand("Update nhanvien set  nvten = '" + nvten + "',nvdiachi= '" + nvdiachi + "',nvsdt = " + nvsdt + ",nvemail = '" + nvemail + "',nvgioitinh = '" + nvgioitinh + "' where manv=" + man + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "nhanvien");
            cmb = new SqlCommandBuilder(da);
            //dataGridView1.DataSource = ds.Tables[0];
            // this.frmHopdongvayvon_Load();
            //this.LoadForm();
            //this.splitContainer1_Panel2_Paint(sender, e);
            conn.Close();
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemma.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemma.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM nhanvien where manv  = '" + tbtimkiemma.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "nhanvien");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemten.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemten.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM nhanvien where nvten  = '" + tbtimkiemten.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "nhanvien");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


      

        
        
    }
}
